﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using fbasync.webcomponents;
using System.Diagnostics;

namespace fbasync.web
{
    public partial class Dashboard : FacebookPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                uiCounter.Text = Connection.EndGetCount(Connection.BeginGetCount(Connection.UserId.Value, null, null)).ToString();
                uiMultiCounter.Text = Connection.EndMultiGetCount(Connection.BeginMultiGetCount(new[] { Connection.UserId.Value }, null, null)).First().Value.ToString();
            }
        }

        protected void OnSetCounter(object sender, EventArgs e)
        {
            Connection.EndSetCount(Connection.BeginSetCount(int.Parse(uiCounter.Text), null, null));
            Response.Redirect(AppRelativeVirtualPath);
        }

        protected void OnIncrementCounter(object sender, EventArgs e)
        {
            Connection.EndIncrementCount(Connection.BeginIncrementCount(int.Parse(uiCounter.Text), null, null));
            Response.Redirect(AppRelativeVirtualPath);
        }

        protected void OnDecrementCounter(object sender, EventArgs e)
        {
            Connection.EndDecrementCount(Connection.BeginDecrementCount(int.Parse(uiCounter.Text), null, null));
            Response.Redirect(AppRelativeVirtualPath);
        }

        protected void OnSetMultiCounter(object sender, EventArgs e)
        {
            Connection.EndMultiSetCount(Connection.BeginMultiSetCount(new Dictionary<long, int> { { Connection.UserId.Value, int.Parse(uiMultiCounter.Text) } }, null, null));
            Response.Redirect(AppRelativeVirtualPath);
        }

        protected void OnIncrementMultiCounter(object sender, EventArgs e)
        {
            Connection.EndMultiIncrementCount(Connection.BeginMultiIncrementCount(new[] { Connection.UserId.Value }, null, null));
            Response.Redirect(AppRelativeVirtualPath);
        }

        protected void OnDecrementMultiCounter(object sender, EventArgs e)
        {
            Connection.EndMultiDecrementCount(Connection.BeginMultiDecrementCount(new[] { Connection.UserId.Value }, null, null));
            Response.Redirect(AppRelativeVirtualPath);
        }

        protected void OnAddNews(object sender, EventArgs e)
        {
            Connection.EndAddNews(Connection.BeginAddNews(new NewsItem { Image = newsImageUrl.Text, News = new List<News> { new News { Message = newsMessage.Text, Action_Link = new ActionLink { text = newsActionText.Text, href = newsActionHref.Text } } } }, null, null));
            Response.Redirect(AppRelativeVirtualPath);
        }

        protected void OnGetNews(object sender, EventArgs e)
        {
            var item = Connection.EndGetNews(Connection.BeginGetNews(null, null)).Values.FirstOrDefault();
            if (item == null) return;

            newsImageUrl.Text = item.Image;
            var news = item.News.First();
            newsMessage.Text = news.Message;
            newsActionHref.Text = news.Action_Link.href;
            newsActionText.Text = news.Action_Link.text;
        }

        protected void OnClearNews(object sender, EventArgs e)
        {
            Connection.EndClearNews(Connection.BeginClearNews(null, null));
            Response.Redirect(AppRelativeVirtualPath);
        }

        protected void OnMultiAddNews(object sender, EventArgs e)
        {
            Connection.EndMultiAddNews(Connection.BeginMultiAddNews(new[] { Connection.UserId.Value }, new NewsItem { Image = newsImageUrl.Text, News = new List<News> { new News { Message = newsMessage.Text, Action_Link = new ActionLink { text = newsActionText.Text, href = newsActionHref.Text } } } }, null, null));
            Response.Redirect(AppRelativeVirtualPath);
        }

        protected void OnMultiGetNews(object sender, EventArgs e)
        {
            var item = Connection.EndMultiGetNews(Connection.BeginMultiGetNews(new Dictionary<long, IEnumerable<long>> { { Connection.UserId.Value, null } }, null, null))[Connection.UserId.Value].Values.FirstOrDefault();
            if (item == null) return;

            newsImageUrl.Text = item.Image;
            var news = item.News.First();
            newsMessage.Text = news.Message;
            newsActionHref.Text = news.Action_Link.href;
            newsActionText.Text = news.Action_Link.text;
        }

        protected void OnMultiClearNews(object sender, EventArgs e)
        {
            var dict = Connection.EndMultiClearNews(Connection.BeginMultiClearNews(new Dictionary<long, IEnumerable<long>> { { Connection.UserId.Value, null } }, null, null));
            if (dict.Count > 0)
                Debug.Assert(dict[Connection.UserId.Value].Values.FirstOrDefault());
            Response.Redirect(AppRelativeVirtualPath);
        }

        protected void OnAddGlobalNews(object sender, EventArgs e)
        {
            Connection.EndAddGlobalNews(Connection.BeginAddGlobalNews(new NewsItem { Image = newsImageUrl.Text, News = new List<News> { new News { Message = newsMessage.Text, Action_Link = new ActionLink { text = newsActionText.Text, href = newsActionHref.Text } } } }, null, null));
            Response.Redirect(AppRelativeVirtualPath);
        }

        protected void OnGetGlobalNews(object sender, EventArgs e)
        {
            var item = Connection.EndGetGlobalNews(Connection.BeginGetGlobalNews(null, null)).Values.FirstOrDefault();
            if (item == null) return;

            newsImageUrl.Text = item.Image;
            var news = item.News.First();
            newsMessage.Text = news.Message;
            newsActionHref.Text = news.Action_Link.href;
            newsActionText.Text = news.Action_Link.text;
        }

        protected void OnClearGlobalNews(object sender, EventArgs e)
        {
            Connection.EndClearGlobalNews(Connection.BeginClearGlobalNews(null, null));
            Response.Redirect(AppRelativeVirtualPath);
        }

        protected void OnPublishActivity(object sender, EventArgs e)
        {
            var id = Connection.EndPublishActivity(Connection.BeginPublishActivity(new News { Message = activityMessage.Text, Action_Link = new ActionLink { text = activityActionText.Text, href = activityActionHref.Text } }, null, null));
            Debug.Assert(id > 0);
            Response.Redirect(AppRelativeVirtualPath);
        }

        protected void OnGetActivity(object sender, EventArgs e)
        {
            var news = Connection.EndGetActivity(Connection.BeginGetActivity(null, null)).Values.FirstOrDefault();
            if (news == null) return;

            activityMessage.Text = news.Message;
            activityActionHref.Text = news.Action_Link.href;
            activityActionText.Text = news.Action_Link.text;
        }

        protected void OnRemove(object sender, EventArgs e)
        {
            Connection.EndRemoveActivity(Connection.BeginRemoveActivity(Connection.EndGetActivity(Connection.BeginGetActivity(null, null)).Keys, null, null));
            Response.Redirect(AppRelativeVirtualPath);
        }
    }
}
